from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
# Create your views here.
from .models import Question,QuestionType
import random
import sys
sys.path.append(r'../')
from user.models import PaperName,User



    
def paper_names(request,user_id):
    userid = User.objects.get(userid = user_id)
    paper_name_set = userid.papername_set.all()
    if len(paper_name_set) == 0:
        return HttpResponse('目前你还没有创建试卷呢，快返回创建吧')
    else:
        context = {'paper_name_set':paper_name_set,'user_id':user_id}
        return render(request,'paper/paper_names.html',context)


def exam(request,user_id,paper_name,subject_id):
    userid = User.objects.get(userid = user_id)
    paper_n = userid.papername_set.get(paper_name_text = paper_name)
    subject_obj = paper_n.subject#题科目
    topic_list = paper_n.userdaan_set.all()[0]
    topic_bank = eval(topic_list.question_bank)
    topic_amount = eval(topic_list.question_amount)
    topic_score = eval(topic_list.question_score)
    loca = -1
    for y in topic_amount[:]:
        loca = topic_amount.index(y)
        if y == '':
            topic_bank.pop(loca)
            topic_amount.pop(loca)
            topic_score.pop(loca)
        else:
            topic_amount[loca] = int(topic_amount[loca])
            topic_score[loca] = int(topic_score[loca])


    zongfen = sum(topic_score)
    

    question_set = []
    question_id_list = []
    for i in range(len(topic_bank)):
        question_set.append(random.sample(list(subject_obj.questiontype_set.get(question_type_text = topic_bank[i]).question_set.all()),topic_amount[i]))
    for s in question_set:
        for g in s:
            question_id_list.append(g.id)
    context = {'user_id':user_id,'zongfen':zongfen,'topic_score':topic_score,'question_set':question_set,'topic_bank':topic_bank,'question_id_list':question_id_list,'topic_amount':topic_amount}
    return render(request,'paper/exam.html',context)






def paper_name(request,user_id,stu_user_id):
    userid = User.objects.get(userid = user_id)
    paper_name_set = userid.papername_set.all()
    if len(paper_name_set) == 0:
        return HttpResponse('目前你还没有创建试卷呢，快返回创建吧')
    else:
        context = {'paper_name_set':paper_name_set,'user_id':user_id,'stu_user_id':stu_user_id}
        return render(request,'paper/paper_name.html',context)








def exams(request,user_id,paper_name,subject_id,stu_user_id):
    userid = User.objects.get(userid = user_id)
    paper_n = userid.papername_set.get(paper_name_text = paper_name)
    subject_obj = paper_n.subject#题科目
    topic_list = paper_n.userdaan_set.all()[0]
    topic_bank = eval(topic_list.question_bank)
    topic_amount = eval(topic_list.question_amount)
    topic_score = eval(topic_list.question_score)
    loca = -1
    for y in topic_amount[:]:
        loca = topic_amount.index(y)
        if y == '':
            topic_bank.pop(loca)
            topic_amount.pop(loca)
            topic_score.pop(loca)
        else:
            topic_amount[loca] = int(topic_amount[loca])
            topic_score[loca] = int(topic_score[loca])


    zongfen = sum(topic_score)
    

    question_set = []
    question_id_list = []
    for i in range(len(topic_bank)):
        question_set.append(random.sample(list(subject_obj.questiontype_set.get(question_type_text = topic_bank[i]).question_set.all()),topic_amount[i]))
    for s in question_set:
        for g in s:
            question_id_list.append(g.id)
    context = {'user_id':user_id,'stu_user_id':stu_user_id,'zongfen':zongfen,'topic_score':topic_score,'question_set':question_set,'topic_bank':topic_bank,'question_id_list':question_id_list,'topic_amount':topic_amount,'paper_name':paper_name}
    return render(request,'paper/exams.html',context)


















